import {Context, Next} from 'koa'
import AdminService from '../service/admin.service'

class AdminController {
    async downLoadDate(ctx:Context) {
        let data = await AdminService.downLoadDate()
        ctx.body = {
            code: 200,
            msg: "获取日期成功",
            data
        }
    }

    // 下载数据
    async getData(ctx:Context) {
        let pageSize = ctx.request.query.pageSize  || 10
        let page = ctx.request.query.page|| 1
        let queryDate = ctx.request.query.queryDate || ''
        let data = await AdminService.getDataList(pageSize,page,queryDate)
        ctx.body = {
            code: 200,
            msg: "获取数据成功",
            data
        }
    }

    //下载传感器的运行数据并且导入成为excel
    async downloadRunData(ctx:any) {
        let date = ctx.request.body.date // 下载的日期
        let result = await AdminService.downloadRunData(date) // 下载数据
        ctx.body = {
            code: 200,
            msg: "获取数据成功",
            data: result
        }
    }

    async getItem(ctx:Context) {
        let date = ctx.request.query.date
        let res = await AdminService.getItem(date)

        ctx.body = {
            code: 200,
            msg: "获取成功",
            total: res
        }
    }
    // 保存运行是的数据
    async saveDataList(ctx:any) {
        let data = ctx.request.body
        let runData = {
             module_id: data.module_id,
             uvc:data.uvc,
             inf: data.inf,
             tem: data.tem,
             hum: data.hum,
             pre: data.pre
        }
        let statusData = {
            module_id: data.module_id,
            uvc_sta: data.uvc_sta,
            ir_sta: data.ir_sta,
            temp_sta: data.temp_sta,
            hum_sta: data.hum_sta,
            pre_sta: data.pre_sta
        }
        await AdminService.saveDataList(runData)
        await AdminService.saveStatusList(statusData)
        ctx.body = {
            code: 200,
            msg: "添加数据成功"
        }
    }
    // 添加运行时间
    async addRunDate(ctx:Context) {
        ctx.body = {
            code: 200,
            msg: "添加运行时间成功"
        }
    }
    // // 添加模拟数据
    // async addMockData(ctx:any) {
    //     await AdminService.addMockData()
    //     ctx.body = {
    //         code: 200,
    //         msg: "添加数据成功"
    //     }
    // }
    //
    // // 获取历史数据
    // async getHistory(ctx:Context) {
    //     let id = ctx.query.id // 获取设备的编号
    //     let type = ctx.query.type || ''
    //     let data = new Object()
    //     if (type === 'all') {
    //        data = await AdminService.queryAllDevice()
    //     } else {
    //         data = await AdminService.queryDevice(id)
    //     }
    //     ctx.body = {
    //         code: 200,
    //         msg: "获取历史数据成功",
    //         data
    //     }
    // }
    // // 修改设备的
    // async updateState(ctx:any) {
    //     let obj: Array<{id:number,normal:number,warning:number,error:number}> = ctx.request.body
    //     for(let i=0;i<5;i++) {
    //         await AdminService.saveState(obj[i].id,obj[i].normal,obj[i].warning,obj[i].error)
    //     }
    //     ctx.body = {
    //         code: 200,
    //         msg:"修改成功"
    //     }
    // }
    // // 获取今日的设备运行状态
    // async todayDataStatus(ctx:any) {
    //     let currentDate:string = Tool.getCurrentDate()
    //     let res = await AdminService.queryTotalStatus(currentDate)
    //     ctx.body = {
    //         code: 200,
    //         msg: "获取状态成功",
    //         data: res
    //     }
    // }
    async getStatusList(ctx:any) {
        let pageSize = ctx.request.query.pageSize  || 10
        let page = ctx.request.query.page|| 1
        let queryType = ctx.request.query.queryType || ''
        let queryDate = ctx.request.query.queryDate || ''
        let data = await AdminService.getStatusDataList(pageSize,page,queryDate,queryType)
        ctx.body = {
            code: 200,
            msg: "获取数据成功",
            data
        }
    }

    // 获取模块的数量
    async getModuleNum(ctx:Context) {
       let result = await AdminService.getModuleNum()
        ctx.body = {
            code: 200,
            msg: "获取成功",
            data: result
        }
    }
    // 获取分析曲线
    async getAnayleRecord(ctx:Context) {
        let startTime:any = ctx.query.startTime // 获取开始的时间
        let endTime:any = ctx.query.endTime //获取结束的时间
        let module_id:any = ctx.query.module_id // 那个模块
        let module_type:any = ctx.query.module_type // 那个传感器
        let selectDate:any = ctx.query.selectDate // 选择的日期
        let result = await AdminService.getAnayleRecord(startTime,endTime,module_id,module_type,selectDate)
        ctx.body = {
            code: 200,
            msg: "获取数据成功",
            data: result
        }
    }
    // 获取历史曲线
    async getHistoryData(ctx:Context) {
        let module_id = ctx.query.module_id // 模块编号
        let type = ctx.query.type //模块类型
        let data = await AdminService.getHistryList(module_id,type)
        ctx.body = {
            code: 200,
            msg: "获取历史数据成功",
            data
        }
    }
    // 对数据库进行优化
    //是否可以删除数据库数据
    async isDel(ctx:Context,next:Next) {
        let bool:boolean = await AdminService.isDel()
        if (bool) return await next()
         ctx.body = {
             code: 304,
             msg: "数据量还没到七天，不需要删除"
         }
    }
    async deleteData(ctx:Context) {
        let result = await AdminService.deleteData()
        ctx.body = {
            code: 200,
            msg: "删除成功"
        }
    }
    // 获取报警列表
    async getErrorList(ctx:Context) {
        let pageSize: any = ctx.request.query.pageSize  || 10
        let page: any = ctx.request.query.page || 1
        let queryDate:any = ctx.request.query.queryDate || ''
        let queryType:any = ctx.request.query.queryType || ''
        let keywords:any = ctx.request.query.keywords || ''
        let list = await AdminService.getErrorList(page,pageSize,queryDate,queryType,keywords)
        ctx.body = {code: 200,msg: "获取数据成功",data: list}
    }

    // 添加报警信息
    async addErrorList(ctx:any) {
        let module_id: number = ctx.request.body.module_id //模块编号
        let info: string = ctx.request.body.info //报警内容
        let level: number = ctx.request.body.level //报警等级
        let date: string = ctx.request.body.date // 字符串
        let state: number = ctx.request.body.state // 状态
        let error_value: number = ctx.request.body.error_value //报警的值
        let type_id: number = ctx.request.body.type_id  // 类型
        let res = await AdminService.addErrorInfo(module_id,info,level,date,state,error_value,type_id)
        if (res) {
            ctx.body = {code: 200,msg: "添加报警信息成功"}
        } else {
            ctx.body = {code: 304,msg: "添加失败"}
        }

    }
    // 删除报警信息
    async removeErrorInfo(ctx:Context) {
        let rid:any = ctx.request.query.rid
        let res: number = await AdminService.removeErrorRecord(rid)
        if (res) {
            ctx.body = {code: 200,msg: "删除成功"}
        } else {
            ctx.body = {code: 304,msg: "删除失败"}
        }
    }

    // 修改状态
    async updateErrorState(ctx:any) {
        let state: number = ctx.request.body.state
        let rid: number = ctx.request.body.rid
        let result = await AdminService.updateErrorState(rid,state)
        if (result) {
            ctx.body = {code: 200,msg: "修改状态成功"}
        } else {
            ctx.body = {code: 500,msg: "修改失败"}
        }
    }

    async getErrorNum(ctx:Context) {
        let {today,week,month} = ctx.query
        let result = await AdminService.getErrorNum(today,week,month)
        ctx.body = {
            code: 200,
            msg: "获取成功",
            data: result
        }
    }
    // 获取监测点报警的排行列表
    async getSortPoint(ctx:Context) {
        let result = await AdminService.getSortPoint()

        ctx.body = {
            code: 200,
            msg: "获取数据成功",
            data: result
        }
    }
}


export default new AdminController
